package com.sias.reflection;

import com.sias.Cat;

import java.lang.reflect.Method;

/**
 * @author 吴文杰
 * @version 1.0
 * @createTime 2023-05-02 12:53:13
 */
public class Reflection02 {

  static final Integer MAX_VALUE = 100000000;

  public static void main(String[] args) throws Exception {

    long startTime = System.currentTimeMillis();
    // m1();//4s
    m2();//未优化145s  优化后94s
    long endTime = System.currentTimeMillis();
    System.out.println("----constTime:"+(endTime-startTime)+"毫秒");
  }

  public static void m1(){
    Cat cat = new Cat();
    for (int i = 0; i < MAX_VALUE; i++) {
      cat.hi();
    }
  }

  public static void m2() throws Exception {
    Class<?> cls = Class.forName("com.sias.Cat");
    Object cat = cls.newInstance();
    Method method = cls.getMethod("hi");
    method.setAccessible(true);
    for (int i = 0; i < MAX_VALUE; i++) {
      method.invoke(cat);
    }
  }

}
